import request from "/utils/omsRequest";
import { bus, moment } from "/utils/core";
import validator from "/utils/validators";

Component({
  mixins: [],
  data: {
    freeServiceList: [],
    history: {
      agree: [],
      review: [],
      refuse: []
    },
    tabKey: null,
    form: {
      amount: null,
      terminationDate: null,
      payeeName: null,
      payeeAccount: null,
      payeeAccountNumber: null,
      refundRemark: null,
      emailEnclosureList: []
    }
  },
  props: {
    show: false,
    currentItem: null,
    onClose: () => { },
  },
  didMount() {
    bus.subscribe('queryRefundRecords', (contract) => {
      this.setData({
        "form.emailEnclosureList": []
      })
      dd.showLoading({
        content: '加载中...'
      })
      request({
        url: "/ajax/contractRefundRecord/refundRecordList",
        data: {
          contractId: contract.id
        }
      }).then(res => {
        request({
          url: '/ajax/contractFreeService/list',
          data: {
            freeServiceType: 'FREE',
            contractGroupId: contract.contractGroupId
          }
        }).then(res => {
          dd.hideLoading()
          this.setData({
            freeServiceList: res.voList
          })
        })
        this.setData({
          history: {
            agree: res.voList.filter(item => item.approvalStatus === 'AGREE'),
            review: res.voList.filter(item => item.approvalStatus === 'REVIEW'),
            refuse: res.voList.filter(item => item.approvalStatus === 'REFUSE')
          }
        })
        this.setData({
          tabKey: this.data.history.agree.length > 0 ? 'agree' : this.data.history.review.length > 0 ? 'review' : 'refuse'
        })
      })
    })
  },
  didUpdate() { },
  didUnmount() {
    bus.remove('queryRefundRecords')
  },
  methods: {
    catchMethod() { },
    // 显示datepicker
    showDatePicker() {
      dd.datePicker({
        format: 'yyyy-MM-dd',
        currentDate: moment().format('YYYY-MM-DD'),
        success: (res) => {
          if (res.date) {
            this.setData({
              "form.terminationDate": res.date
            })
          }
        },
      });
    },
    // 关闭modal
    hideModal(e) {
      this.setData({
        freeServiceList: [],
        history: {
          agree: [],
          review: [],
          refuse: []
        },
        tabKey: null,
        form: {
          amount: null,
          terminationDate: null,
          payeeName: null,
          payeeAccount: null,
          payeeAccountNumber: null,
          refundRemark: null,
          emailEnclosureList: []
        }
      })
      this.props.onClose(e)
    },
    // tab切换
    onTabSelect(e) {
      this.setData({
        tabKey: e.target.dataset.key
      })
    },
    onFormSubmit(e) {
      Object.assign(this.data.form, e.detail.value);
      this.setData({
        "form.contractId": this.props.currentItem.product.id,
        "form.amount": parseFloat(this.data.form.amount)
      })
      let validate = new validator();
      validate.add("checkEmpty", this.data.form.terminationDate, "请选择终止合作日期");
      validate.add("checkIntNotZero", this.data.form.amount);
      validate.add("checkEmpty", this.data.form.payeeName, "请输入收款人姓名");
      validate.add("checkEmpty", this.data.form.payeeAccount, "请输入收款人账户");
      validate.add("checkEmpty", this.data.form.payeeAccountNumber, "请输入收款人账号");
      validate.add("checkEmpty", this.data.form.refundRemark, "请输入退款原因");
      validate.add("checkEmptyArray", this.data.form.emailEnclosureList, "请上传审批邮件");
      validate.add("checkEmpty", this.data.form.contractId, "合同id不能为空");
      if (validate.check()) {
        dd.showLoading({
          content: "加载中..."
        })
        request({
          url: "/ajax/contractRefundRecord/createContractRefundRecord",
          method: "post",
          headers: {
            "Content-Type": "application/json"
          },
          data: this.data.form
        }).then(() => {
          dd.hideLoading()
          this.hideModal({ target: { dataset: { show: false, init: true } } })
        })
      }
    },
    onFileRemove(index) {
      this.data.form.emailEnclosureList.splice(index, 1)
      this.setData({
        "form.emailEnclosureList": this.data.form.emailEnclosureList
      })
    },
    onFileChange(files) {
      this.data.form.emailEnclosureList.push(...files)
      this.setData({
        "form.emailEnclosureList": this.data.form.emailEnclosureList
      })
    }
  },
});
